package com.totoro.project.analysis ;

import java.io.BufferedInputStream ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.io.InputStream ;
import java.util.HashMap ;
import java.util.Map ;

import javax.xml.parsers.ParserConfigurationException ;
import javax.xml.parsers.SAXParser ;
import javax.xml.parsers.SAXParserFactory ;

import org.xml.sax.Attributes ;
import org.xml.sax.SAXException ;
import org.xml.sax.helpers.DefaultHandler ;

public class AnalysisXmlImpl implements IAnalysis {
    
    @Override
    public Map<String, String> getEnvironmentKV(InputStream is) {
        SAXParserFactory spf = SAXParserFactory.newInstance() ;
        Map<String, String> propertiesMap = null ;
        try {
            SAXParser saxParser = spf.newSAXParser() ;
            propertiesMap = new HashMap<String, String>() ;
            XmlParseHandler handler = new XmlParseHandler(propertiesMap) ;
            saxParser.parse(is, handler) ;
        } catch (ParserConfigurationException e) {
            e.printStackTrace() ;
        } catch (SAXException e) {
            e.printStackTrace() ;
        } catch (IOException e) {
            e.printStackTrace() ;
        } catch (Exception e) {
            e.printStackTrace() ;
        }
        return propertiesMap ;
    }
    
    class XmlParseHandler extends DefaultHandler {
        private boolean propertieTag, nameTag, valueTag ;
        private String propertieName ;
        private String propertieValue ;
        Map<String, String> propertiesMap ;
        
        protected XmlParseHandler(Map<String, String> propertiesMap) {
            this.propertiesMap = propertiesMap ;
        }
        
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (propertieTag) {
                String context = new String(ch, start, length) ;
                if (nameTag) {
                    propertieName = context ;
                } else if (valueTag) {
                    propertieValue = context ;
                }
            }
        }
        
        @Override
        public void startDocument() throws SAXException {
            super.startDocument() ;
            System.out.println("begin parse xml! only parse that tag : <propertie><name>propertieName</name><value>propertieValue</value></propertie>") ;
        }
        
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if (qName.equals("propertie")) {
                propertieTag = true ;
            } else if (qName.equals("name")) {
                nameTag = true ;
            } else if (qName.equals("value")) {
                valueTag = true ;
            }
            
        }
        
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (qName.equals("propertie")) {
                propertieTag = false ;
                if (propertieName != null && propertieValue != null)
                    propertiesMap.put(propertieName, propertieValue) ;
                propertieName = null ;
                propertieValue = null ;
                nameTag = false ;
                valueTag = false ;
            } else if (qName.equals("name")) {
                nameTag = false ;
            } else if (qName.equals("value")) {
                valueTag = false ;
            }
        }
        
        @Override
        public void endDocument() throws SAXException {
            super.endDocument() ;
            System.out.println("parse xml end!") ;
        }
    }
    
    public static void main(String[] args) throws FileNotFoundException {
        AnalysisXmlImpl test = new AnalysisXmlImpl() ;
        InputStream is = new BufferedInputStream(new FileInputStream(new File("E:/testFile/propertes.xml"))) ;
        Map<String, String> tet = test.getEnvironmentKV(is) ;
        System.out.println(tet) ;
    }
    
}
